package com.baozun.houji.me.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.model.enums.VerifyCodeType
import com.baozhun.mall.common.network.mCommonApiService
import com.baozun.houji.me.R
import com.baozhun.mall.common.model.bean.ResetPwdReqBean
import com.baozun.houji.me.network.mMeApiService
import com.blankj.utilcode.util.ToastUtils
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/12/9
 * @Version：1.0.0
 */
class ForgetPwdViewModel : BaseViewModel() {
    var email = MutableLiveData("")
    var ticket = ""
    var remainingTime = MutableLiveData<Int>()
    var code = MutableLiveData("")

    /**
     * 获取验证码
     */
    fun getVerifyCode(getSuccess: () -> Unit) {
        checkParams(false) {
            request({
                mCommonApiService.sendEmailVerifyCode(
                    ResetPwdReqBean(
                        email = email.value.toString(),
                        type = VerifyCodeType.RESET_PWD.type
                    )
                )
            }, {
                ticket = it?.ticket.toString()
                getSuccess.invoke()
            }, isShowDialog = true)
        }

    }


    /**
     * 校验验证码
     */
    fun checkVerifyCode(operationSuccess: () -> Unit) {
        checkParams {
            request({
                mMeApiService.checkVerifyCode(
                    ResetPwdReqBean(
                        email.value.toString(),
                        code.value.toString(),
                        VerifyCodeType.RESET_PWD.type,
                        ticket
                    )
                )
            }, { it ->
                operationSuccess.invoke()
            }, isShowDialog = true, showSuccessMsg = false)
        }
    }

    private fun checkParams(checkVerifyCode: Boolean = false, checkPass: () -> Unit) {
        if (email.value.isNullOrEmpty()) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_input_email)
            return
        }
        if (checkVerifyCode && code.value.isNullOrEmpty()) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_input_verify_code)
            return
        }

        checkPass.invoke()
    }


}